package com.app.qtjk.lsh.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.app.common.utils.CollectionUtil;
import com.app.common.utils.ReqJsonUtil;
import com.app.common.utils.StringUtil;
import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;

@ServiceContainer
public class AppVersionService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(AppVersionService.class);

	public AppVersionService() throws Exception {
		super();
	}


	/**
	 * 用户点击版本更新
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "AppVersionService_userUpdateApp")
	public IResData userUpdateApp(IReqData req) throws Exception {

		

		log.debug("开始进入 AppVersionService_userUpdateApp 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		IResData res = new SwordRes();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();
		HashMap hashMap = new HashMap();
		try {
			SQLParam sqlParam = new SQLParam();
			// 获取前台post的数据
			String phonetype = ReqJsonUtil.getPostParam(req, "phonetype");
			String appversionNumber = ReqJsonUtil.getPostParam(req, "appversionNumber");
			
			sqlParam.addParam(phonetype);
			//获取某类型手机的应用的所支持的最低版本号
			String MinAppversionNum = StringUtil.parse2String(dao.queryColumnByKey("AppVersionManageService_getMinAppVersionByPhoneType", sqlParam, 1));
			sqlParam.addParam(phonetype);
			//获取某类型手机的应用的最高版本号
			Map<String, Object> AppVersionMap = dao.queryMapByKey("AppVersionManageService_getAppVersionByMaxNum", sqlParam);
			Map appVersionData=new HashMap();
			if(CollectionUtil.isNotBlankMap(AppVersionMap)){
				String MaxAppversionNum=StringUtil.parse2String(AppVersionMap.get("appversionNumber"));
				//比较版本号，字符串比较
				int a = appversionNumber.compareToIgnoreCase(MinAppversionNum);
				int b = appversionNumber.compareToIgnoreCase(MaxAppversionNum);
				if (a<0) {
					appVersionData.put("sfNeedUpdate", "0");//必须更新
					appVersionData.put("UpdateExplain", "必须更新");
					appVersionData.putAll(AppVersionMap);
				}
				if (a>=0 && b<0) {
					appVersionData.put("sfNeedUpdate", "1");//可以更新
					appVersionData.put("UpdateExplain", "可以更新");
					appVersionData.putAll(AppVersionMap);
				}
				if (b>=0) {
					appVersionData.put("sfNeedUpdate", "2");//无需更新
					appVersionData.put("UpdateExplain", "无需更新");
				}
				commonMap.put("appVersionData", appVersionData);
				hashMap.put("returnStatus", "true");
				hashMap.put("msg", "成功返回，未出现异常！");
			}else{
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "获取系统应用最高版本出现异常！");
			}
		} catch (Exception e) {
			log.debug("---------------系统异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;

	}


}